package gov.pbc.nn.kjgl.maintenance.handler;

import gov.pbc.nn.core.dao.CommonJdbcDao;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.AssignmentHandler;
import com.bstek.uflo.process.node.TaskNode;

/**
 * 系统业务主管部门业务人员
 * 
 * @author Administrator
 * 
 */
@Component(SysBusinessPeopleAssignmentHandler.BEAN_ID)
public class SysBusinessPeopleAssignmentHandler implements AssignmentHandler {

	public static final String BEAN_ID = "gov.pbc.nn.kjgl.maintenance.handler.SysBusinessPeopleAssignmentHandler";

	@Resource(name = CommonJdbcDao.BEAN_ID)
	private CommonJdbcDao jdbcDao;

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreDao;

	@Override
	public Collection<String> handle(TaskNode node, ProcessInstance pi, Context context) {
		String sysId = (String) context.getProcessService().getProcessVariable("sysId", pi);
		List<String> users = new ArrayList<String>();
		List<String> backUsers = (List<String>) context.getProcessService().getProcessVariable("backUsers", pi);// 被退回重新给出意见的业务员
		if (backUsers == null || backUsers.isEmpty()) {
			users = this.getBusinessPeople(sysId);
		} else {
			users = backUsers;
		}
		return users;
	}

	/**
	 * 获取系统业务主管部门业务员
	 * 
	 * @param businessId
	 * @return
	 */
	public List<String> getBusinessPeople(String sysId) {
		String sql = "select c.USERNAME_ from t_sys_clerk c where c.SYS_ID_=?";
		List<String> users = this.jdbcDao.getJdbcTemplate().query(sql, new Object[] { sysId }, new RowMapper<String>() {

			@Override
			public String mapRow(ResultSet rs, int rowNum) throws SQLException {
				return rs.getString("USERNAME_");
			}

		});
		return users;
	}
}
